package com.site.user.service;


import java.util.ArrayList;
import java.util.List;


import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;
import com.site.data.JPABaseDAOImpl;
import com.site.user.pojo.TbPrivilegeTemp;
import com.site.user.pojo.TbPrivilegeTempPK;


/**
 * @author      zhanjun
 * @date        2010-12-8
 * @version     1.0
 * @description 用户表服务层
 * 
 */
@Service("tbPrivilegeTemp")
public class TbPrivilegeTempService extends JPABaseDAOImpl<TbPrivilegeTemp> implements ITbPrivilegeTempService {


	public String createPrivilegeTemp(String idrole, String idsystem, String idOperation, String idResource) {
		
		if ((StringUtils.isNotEmpty(idResource)) || (StringUtils.isNotEmpty(idOperation))) {
			return null;
		}
		String[] aryidres = idResource.split(",");
		String[] aryidoptn = idOperation.split(",");
	
		List<TbPrivilegeTemp> entitylist=new ArrayList<TbPrivilegeTemp>();
		
		for (int i = 0; i < aryidres.length; i++) {
			for (int j = 0; j < aryidoptn.length; j++) {
				String testsql = "idRole='"+ idrole+ "' and idSystem='"+ idsystem+ "' and idResource='"+ aryidres[i]+ "' and idOperator='" + aryidoptn[j]+"'";
				Long count=queryCount(testsql, null);
				// 先检查是否已经有重复的数据
			
					if (count==0) {// 没有重复的数据的插入
						TbPrivilegeTemp entity=new TbPrivilegeTemp();
						TbPrivilegeTempPK pk=new TbPrivilegeTempPK();
						
						pk.setIdOperator(aryidoptn[j]);
						pk.setIdResource(aryidres[i]);
						pk.setIdRole(idrole);
						entity.setTbPrivilegeTempPK(pk);
						entity.setIdSystem(idsystem);
						entitylist.add(entity);
					}
				
			}
		}
		if(entitylist.size()>0){
		// 大数据量插入
			jdbcSaveEntityBatch(entitylist);
		}
		return "保存成功！";
		
	}
	public String deleteTempPrivilege(String ids)
	{
		String[] id=ids.split("-");
		String delsql = "delete FROM tb_privilege_temp where ID_ROLE='"
			+ id[0]
			+ "' and ID_SYSTEM='"
			+ id[1]
			+ "' and ID_RESOURCE='"
			+ id[2]
			+ "' and ID_OPERATOR='" + id[3]+"'";
		int i=jdbcExecUpdate(delsql);
		
		return i+"条数据删除成功!";
	}

	

}
